package model

import (
	"mlgmd.com/hearsay/base"
	"mlgmd.com/hearsay/entities"
	"mlgmd.com/hearsay/utils"
	"xorm.io/xorm"
)

type HearsayListItem struct {
	entities.Hearsay `xorm:"extends"`
	Creator          entities.User          `xorm:"extends" json:"creator"`
	Unlock           entities.HearsayUnlock `xorm:"extends" json:"unlock"` // 当前用户是否解锁此传闻
	Browse           entities.HearsayBrowse `xorm:"extends" json:"browse"`
}

func (HearsayListItem) Query(DB xorm.Interface, uid int) *xorm.Session {
	return DB.Table([]any{&entities.Hearsay{}, "h"}).
		Join("INNER", []any{&entities.User{}, "u"}, "u.id = h.creator_id").
		Join("LEFT", []any{entities.HearsayUnlock{}, "hu"}, "hu.user_id=? and hu.hearsay_id=h.id", uid).
		Join("LEFT", []any{entities.HearsayBrowse{}, "hb"}, "hb.user_id=? and hb.hearsay_id=h.id", uid)
}

type Hearsay struct {
	entities.Hearsay `xorm:"extends"`
	Creator          entities.User          `xorm:"extends" json:"creator"`
	Comments         []HearsayComment       `json:"comments"`
	Unlock           entities.HearsayUnlock `json:"unlock"`         // 当前用户是否解锁此传闻
	ProtectedEmpty   bool                   `json:"protectedEmpty"` // 隐藏内容为空
}

func (h *Hearsay) AfterLoad() {
	h.Hearsay.AfterLoad()
	if h.Comments == nil {
		h.Comments = make([]HearsayComment, 0)
	}
}

func (Hearsay) Get(DB xorm.Interface, id int) (data Hearsay, err error) {
	defer base.Error(&err)
	base.Has(base.Must2(DB.ID(id).Get(&data.Hearsay)))
	base.Must2(DB.ID(data.CreatorId).Get(&data.Creator))
	base.Must(HearsayComment{}.Query(DB).Where("c.target_id = ? && c.deleted_time = 0 && c.blocked = 0", data.Id).Find(&data.Comments))
	data.AfterLoad()
	cm := utils.MapByKey(data.Comments)
	for i, v := range data.Comments {
		if c, has := cm[v.ReplyToId]; has {
			data.Comments[i].ReplyTo = c.HearsayComment
		}
	}
	return
}

// func (Hearsay) Complete(DB xorm.Interface, data *[]Hearsay) (err error) {
// 	defer base.Error(&err)
// 	hm := make(map[int]int, 0)
// 	for i, h := range *data {
// 		hm[h.Id] = i
// 	}
// 	cs := make([]HearsayComment, 0)
// 	base.Must(HearsayComment{}.Query(DB).In("c.target_id", utils.Keys(hm)).Find(&cs))
// 	for _, c := range cs {
// 		(*data)[hm[c.TargetId]].Comments = append((*data)[hm[c.TargetId]].Comments, c)
// 	}
// 	return
// }

type HearsayComment struct {
	entities.HearsayComment `xorm:"extends"`
	Creator                 entities.User           `xorm:"extends" json:"creator"`
	ReplyTo                 entities.HearsayComment `json:"replyTo"`
	Unlocked                bool                    `json:"unlocked"` // 评论的用户是否解锁传闻
	Result                  int                     `json:"result"`   // 评论的用户对传闻的评价
}

func (HearsayComment) Query(DB xorm.Interface) *xorm.Session {
	return DB.Table([]any{&entities.HearsayComment{}, "c"}).
		Join("INNER", []any{&entities.User{}, "u"}, "u.id = c.creator_id")
}

type HearsayWithUnlock struct {
	entities.Hearsay `xorm:"extends"`
	Creator          entities.User          `xorm:"extends" json:"creator"`
	Unlock           entities.HearsayUnlock `xorm:"extends" json:"unlock"` // 当前用户是否解锁此传闻
}

func (HearsayWithUnlock) Query(DB xorm.Interface) *xorm.Session {
	return DB.Table([]any{&entities.Hearsay{}, "h"}).
		Join("INNER", []any{&entities.User{}, "u"}, "u.id = h.creator_id").
		Join("INNER", []any{&entities.HearsayUnlock{}, "hu"}, "hu.hearsay_id = h.id")
}
